<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
         //每页显示条数
        $num = $request->input('num',15);

        //检测是否提交查询关键字
        if($request->has('keywords')){
             //分页查询 select * from user where username like '%admin%';
            $goods= DB::table('shangpin')
                              
                                ->leftJoin('sdianpu','shangpin.shopid','=','sdianpu.id')
                                ->leftJoin('stype','shangpin.pid','=','stype.id') 
                                 ->where('shangpin.name','like','%'.$request->input('keywords').'%' )
                                ->select('shangpin.name as sname','sdianpu.name as dname','stype.name as tname','shangpin.brand','shangpin.price','shangpin.num','shangpin.id')
                                ->paginate($num);
     
         
        }else{
            $goods = DB::table('shangpin')
                ->leftJoin('sdianpu','shangpin.shopid','=','sdianpu.id')
                ->leftJoin('stype','shangpin.pid','=','stype.id')
                ->select('shangpin.name as sname','sdianpu.name as dname','stype.name as tname','shangpin.brand','shangpin.price','shangpin.num','shangpin.id') 
               ->paginate($num); 
       
        }
        //dd($goods);
  //var_dump($goods);
        //提取参数 分配到页面中
        $data = $request->except('page');


        //解析模板 分配数据
        return view('Admin.goods.index',['goods'=>$goods,'data'=>$data]);
       
       return view('Admin.goods.index');
        
    }
    public function postDel(Request $request)
    {
        //获取数据
        $id = $request->input('id');
    
        //执行删除
        $res = DB::table('shangpin')->where('id',$id)->delete();

        echo $res;
       
    }
    public function getEdit($id)
    {
        //查询数据库，获取数据
        $goods=DB::table('shangpin')
        ->leftJoin('spinglun','shangpin.id','=','spinglun.sid')
        ->where('shangpin.id',$id)
        ->select('shangpin.name','shangpin.intro','shangpin.photo','shangpin.parameter','spinglun.content','shangpin.id')
        ->get();
     //dd($goods);
        //解析模板
        return view('Admin.goods.edit',['goods'=>$goods]);

    }


    public function postUpdate(Request $request)
    {
          $data = $request->except('_token');

         if ($request->hasFile('photo')) {
             //调用方法进行头像的上传操作
             $data['photo'] = $this->upload($request);
         }

// $array=[];
// foreach($data as $k=>$v)
// {
//     if($k=='content'){
//         $array['spinglun.'.$k]=$v;
        
//     }else{
//         $array['shangpin.'.$k]=$v;
//     }
// }
        //执行数据修改
         $res = DB::table('shangpin') 
                ->join('spinglun','shangpin.id','=','spinglun.sid')
                ->where('shangpin.id',$data['id'])
              ->update(['shangpin.name'=>$data['name'],'spinglun.content'=>$data['content'],'shangpin.intro'=>$data['intro'],'shangpin.parameter'=>$data['parameter'],'shangpin.photo'=>$data['photo']]);
               // ->update($array);
        if($res ){
          
            //重定向到 user/index
            return redirect('admin/goods/index');
        }else{
            //回到上个页面
            return back();
        }




    }
     public function upload($request)
    {
           //检测是否有文件上传
        if ($request->hasFile('photo')) {
            //随机文件名
            $name = md5(time()+rand(1,99999));
            //后缀名
            $su = $request->file('photo')->getClientOriginalExtension();
            $request->file('photo')->move('./public/uploads/shangpin', $name.'.'.$su);
            return '/public/uploads/shangpin/'.$name.'.'.$su;
        }

}

   
}
